"""
爬虫页面状态模型 - 对应 scraper_page_status 表
"""
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Integer, DateTime, Text, Enum, BigInteger
from sqlalchemy.orm import Mapped, mapped_column
from axiom_boot.database.base_model import BaseModel
from axiom_boot.core.types import BigInt


class ScraperPageStatus(BaseModel):
    """爬虫页面状态模型"""
    
    __tablename__ = "scraper_page_status"
    
    # 显式重写 id 以符合【主键覆盖原则】
    id: Mapped[BigInt] = mapped_column(BigInteger, primary_key=True, comment="主键ID (雪花ID)")
    
    # 基本信息
    site_name: Mapped[str] = mapped_column(String(50), nullable=False, comment="网站名称")
    page_number: Mapped[int] = mapped_column(Integer, nullable=False, comment="页面编号")
    per_page: Mapped[int] = mapped_column(Integer, default=100, comment="每页数量")
    
    # 状态信息
    status: Mapped[str] = mapped_column(
        Enum('pending', 'processing', 'completed', 'failed', name='page_status_enum'),
        default='pending',
        comment="状态"
    )
    scraped_count: Mapped[int] = mapped_column(Integer, default=0, comment="成功爬取数量")
    expected_count: Mapped[int] = mapped_column(Integer, default=0, comment="预期数量")
    retry_count: Mapped[int] = mapped_column(Integer, default=0, comment="重试次数")
    last_error: Mapped[Optional[str]] = mapped_column(Text, comment="最后错误信息")
    
    # 任务信息
    task_id: Mapped[Optional[str]] = mapped_column(String(100), comment="任务ID")
    started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, comment="开始时间")
    completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, comment="完成时间")

    def __repr__(self):
        return f"<ScraperPageStatus(site='{self.site_name}', page={self.page_number}, status='{self.status}')>"